/**
 * CC2003
 * 
 * For the course CC2003 - Algorithms and Data Structures of UVG (Universidad del Valle de Guatemala, http://uvg.edu.gt)
 * 
 * 
 * 
 * SVN Repository of my course projects: http://cc2003.svn.sourceforge.net/viewvc/cc2003/
 * 
 * 
 * Copyright (C) 2009 Carlos Lopez Camey
 * visit me at http://kmels.net or write me at c.lopez@kmels.net
 * 
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/> or write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package main;

import java.io.FileInputStream;
import java.io.IOException;

import javax.xml.bind.JAXBException;

import estructuras.FullQueueException;
import estructuras.Queue;
import estructuras.QueueUsingCircularList;

/**
 * @author byron
 *
 */
public class CorrerServidor {

	private Queue<FileInputStream> cola;
	private MoculatorServer moculatorServer;
	
	public void crear() throws JAXBException, PuertoNoEscuchableException, IOException, ClassNotFoundException, FullQueueException {
		moculatorServer = new MoculatorServer();
		cola = new QueueUsingCircularList<FileInputStream>();
		
		Thread hilo_ejecucion = new Ejecucion();
		hilo_ejecucion.start();
		while (true){
			FileInputStream archivoRecibido = moculatorServer.escuchar(3232);
			System.out.println("Recibio archivo"+archivoRecibido);
			if (archivoRecibido != null){
				cola.add(archivoRecibido);
				// hilo_ejecucion.start();
			}
		}

	}
	
	private class Ejecucion extends Thread {
		public void run() {
			try {
				while(true) {
					while(cola.isEmpty()) {
						//wait();
					}

					// Realizar request en cola 
					moculatorServer.getRequest(cola.remove());
				}
			
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}
}
